#include<16f877a.h>
//#device ADC=10
#include<stdio.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define row1() init_lcd(0x80)	/* Begin at Line 1 */
#define row2() init_lcd(0xC0)   /* Begin at Line 2 */
#define LCD_Clear() init_lcd(0x01)	/* Clear display LCD */
unsigned char w[] = { 0x38,0x0c,0x01,0x06,0x80};
//unsigned int bp=0;
int i,c,temperature,temp,dum1,dum2,dum3,dummy,bp,heart,z,flag=0;
int count=0;
/*#define MOT1 PIN_C0
#define MOT2 PIN_C1
#define MOT3 PIN_C2
#define MOT4 PIN_C3
void forward()
 {
output_bit(MOT1,HIGH);
output_bit(MOT2,LOW);
output_bit(MOT3,LOW);
output_bit(MOT4,HIGH);
 }*/
/*======================================================LCD Initialisation============================================*/
void init_lcd(unsigned char j)
{
   output_e (0x00);
   output_d (j);
   output_e (0x02);
   delay_ms (2);
   output_e (0x00);
   delay_ms (2);
}
/*=======================================================Display Function=============================================*/
void display(char s)
{
   output_e (0x01);
   output_d (s);
   output_e (0x03);
   delay_ms (2);
   output_e (0x01);
   delay_ms (2);
}
/*=======================================================Functions For ROW============================================*/
void Lcd_init()
{
unsigned char i=0;
   for( i = 0 ; i < 6 ; i++ )
   {
      init_lcd (w[i]);
   }
}
void send1()
{
printf("at+cmgs=\"+919789035360\"\n");

            putc(0x0d);
            putc(0x0a);
            delay_ms(1000);
}
void send2()
{
printf("at+cmgs=\"+919790675765\"\n");

            putc(0x0d);
            putc(0x0a);
            delay_ms(1000);
}
void call()
{
printf("atd+919789035360;\n");
putc(0x0d);
putc(0x0a);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
}
void ser()
{
printf("at+cmgf=1\n");

           putc(0x0d);
           putc(0x0a);
           delay_ms(1000);
}


void main()
{

      Lcd_init();
      ser();
      while(1)
      { 
      flag=flag+1;
      set_adc_channel(0);
      delay_ms(10);
      temperature=Read_ADC();
      temperature=temperature+17;
      //printf("%d",temp);
      //temperature =  (5 * temp * 256) ;
      //printf("%d\n\r",temperature);

      row1();
      display("Temperature- ");
      delay_ms(100);


         row2();
         dum1=temperature;
         temperature=dum1%1000;
         temperature=temperature/100;
         dummy=temperature+48;
         display(dummy);
         delay_us(10);
         temperature=dum1%100;
         temperature=temperature/10;
         dummy=temperature+48;
         display(dummy);
         //transmit(dummy);
         delay_us(10);
         temperature=dum1%10;
         dummy=temperature+48;
         //transmit(dummy);
         display(dummy);
         delay_ms(10);
         display("deg/cel");
         
         if(dum1>45)
         {
            call();
            delay_ms(1000);
            delay_ms(1000);
         }
      delay_ms(3000);

      LCD_Clear();
      row1();
      display("Blood Pressure - ");
      delay_ms(100);

      set_adc_channel(1);
      delay_us(10);
      bp=Read_ADC();
      //printf("%d",bp);
      if(bp<20)
      {
         bp=bp+103;
         delay_ms(1500);
         
      }
      else if(bp>=20 && bp<=30)
      {

         bp+=92;
         delay_ms(500);
         
      }

      else if(bp>=31 && bp<=40)
      {

         bp+=81;
         delay_ms(500);
            
      }

      else if(bp>=41 && bp<=54)
      {

         bp+=68;
        delay_ms(500);  
      }
         else if(bp>=80 && bp<=90)
      {
      bp+=22;
      delay_ms(500);
      }
         row2();
         dum2=bp;
         bp=dum2%1000;
         bp=bp/100;
         dummy=bp+48;
         display(dummy);
         delay_us(10);
         bp=dum2%100;
         bp=bp/10;
         dummy=bp+48;
         display(dummy);
         //transmit(dummy);
         delay_us(10);
         bp=dum2%10;
         dummy=bp+48;
         //transmit(dummy);
         display(dummy);
         display("Hg/mm"); 
         delay_ms(3000);
         
         
        
         if(dum2>140)
         {
            call();
            delay_ms(1000);
            delay_ms(1000);
         }
         else if(dum2<80)
         {
            call();
            delay_ms(1000);
            delay_ms(1000);
         } 
     

         LCD_Clear();
         row1();
         display("Heart Rate - ");
         delay_ms(100);
         set_adc_channel(2);
         delay_us(10);
         z=100;
         count=0;
         while(z!=0)
         {
         heart=Read_ADC();
         printf("%d",heart);
         if(heart==0)
         {
         count=count+1;
         }
         z--;
         delay_ms(100);
         }
         if(count>90 && count<105)
         {
         count=0;
         }
         delay_ms(100);
         row2();
         dum3=count;
         count=dum3%1000;
         count=count/100;
         dummy=count+48;
         display(dummy);
         delay_us(10);
         count=dum3%100;
         count=count/10;
         dummy=count+48;
         display(dummy);
         //transmit(dummy);
         delay_us(10);
         count=dum3%10;
         dummy=count+48;
         //transmit(dummy);
         display(dummy);
         delay_ms(10);
         display("bpm");
         
        
         delay_ms(1000);
         if(dum3>80)
         {
            call();
            delay_ms(1000);
            delay_ms(1000);
         }
         else if(dum3<60)
         {
            call();
            delay_ms(1000);
            delay_ms(1000);
         }
         delay_ms(3000); 
         LCD_Clear();
      if(flag==5)   
      {
      send1();
      printf("Patient A:\n\r");
      delay_ms(100);
      printf("Temperature is %d deg/c\n\r,",dum1);
      delay_ms(100);
      printf("Blood Pressure is %d Hg/mm,\n\r",dum2);
      delay_ms(100);
      printf("Heart Beat is %d  bpm",dum3);
      delay_ms(100);
      putc(26);
      delay_ms(100);
      
      send2();
      printf("Patient A:\n\r");
      delay_ms(100);
      printf("Temperature is %d deg/c\n\r,",dum1);
      delay_ms(100);
      printf("Blood Pressure is %d Hg/mm,\n\r",dum2);
      delay_ms(100);
      printf("Heart Beat is %d  bpm",dum3);
      delay_ms(100);
      putc(26);
      delay_ms(100);
      flag=0;
      }

}

}
